package com.bjsxt.controller;


import com.bjsxt.pojo.Company;
import com.bjsxt.pojo.PageResult;
import com.bjsxt.pojo.Result;
import com.bjsxt.service.ICompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-02-22
 */
@Controller
@RequestMapping("/company")
public class CompanyController {
    //声明业务层属性
    @Autowired
    private ICompanyService companyService;
    //声明单元方法：分页加载公司信息
    @ResponseBody
    @RequestMapping("companyInfo")
    public PageResult<Company> companyInfo(Integer page,Integer rows,String cname,String status,String ordernumber){
        //处理请求
        PageResult<Company> pageResult=companyService.selCompanyInfoService(page,rows,cname,status,ordernumber);
        //响应结果
        return pageResult;
    }
    //声明单元方法:新增公司
    @ResponseBody
    @RequestMapping("companyAdd")
    public Result companyAdd(Company company){
        //处理请求
        company.setStarttime(LocalDateTime.now());
        company.setStatus("1");
        boolean insert = company.insert();
        //做出响应
        return new Result(insert,insert?"添加公司成功":"添加公司失败");
    }
    //声明单元方法：编辑公司
    @ResponseBody
    @RequestMapping("companyEdit")
    public Result companyEdit(Company company){
        //处理请求
        boolean b = company.updateById();
        //响应结果
        return new Result(b,b?"编辑成功":"编辑失败");
    }
    //声明单元方法：修改公司状态
    @ResponseBody
    @RequestMapping("companyAccountUp")
    public Result companyAccountUp(String cids,String statuss){
        //处理请求
            //获取公司的ID
        String[] cid = cids.split(",");
        String[] status = statuss.split(",");
            //修改
        boolean b = false;
        for (int i=0;i<cid.length;i++){
            Company company=new Company();
            company.setCid(Integer.valueOf(cid[i]));
            company.setStatus("1".equals(status[i])? "0":"1");
            b= company.updateById();
        }
        //响应结果
        return new Result(b,"");
    }
}

